package com.fengke.cn.yejian11.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.activity.AddShaiTuActivity;
import com.fengke.cn.yejian11.adapter.ShaiTuListAdapter;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.fengke.cn.yejian11.javabean.ShaiTuList;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ShowPicFragment extends Fragment {

    private RecyclerView recyclerView;
    private Retrofit retrofit;
    private List<ShaiTuList> shaiTuLists;
    private ShaiTuListAdapter adapter;
    private Button add;

    public ShowPicFragment() {
        // Required empty public constructor
    }



    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        add= (Button) view.findViewById(R.id.shaitu_add);
        ///////////////////////////////////////////////////
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), AddShaiTuActivity.class);
                getContext().startActivity(intent);
            }
        });
        ///////////////////////////////////////////////////
        recyclerView= (RecyclerView) view.findViewById(R.id.shaitu_list);
        final StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        retrofit=new Retrofit.Builder()
                .baseUrl(Config.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ActImpls services=retrofit.create(ActImpls.class);
        retrofit2.Call<List<ShaiTuList>> call=services.getShaiTuList();
        call.enqueue(new Callback<List<ShaiTuList>>() {
            @Override
            public void onResponse(Call<List<ShaiTuList>> call, Response<List<ShaiTuList>> response) {
                List<ShaiTuList> lists=response.body();
                adapter=new ShaiTuListAdapter(getActivity(),lists);
//                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
//                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(adapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<List<ShaiTuList>> call, Throwable t) {

                Toast.makeText(getActivity(),"获取列表失败",Toast.LENGTH_SHORT).show();

            }
        });
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_show_pic, container, false);
    }







}
